package com.library.mapper;

import com.github.pagehelper.Page;
import com.library.annotation.AutoFill;
import com.library.dto.BookPageQueryDTO;
import com.library.dto.SearchPageDTO;
import com.library.entity.Book;
import com.library.enumeration.OperationType;
import com.library.vo.*;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface BookMapper {

    /**
     * 新增图书
     * @param book
     */
    @AutoFill(value = OperationType.INSERT)
    void insert(Book book);

    /**
     * 书籍分页查询
     * @param bookPageQueryDTO
     * @return
     */
    Page<BookPageVO> pageQuery(BookPageQueryDTO bookPageQueryDTO);

    /**
     * 修改图书
     * @param book
     */
    @AutoFill(value = OperationType.UPDATE)
    void update(Book book);

    /**
     * 根据分类id查询图书数量
     * @param id
     * @return
     */
    @Select("select count(id) from book where category_id = #{id}")
    Integer countByCategoryId(Long id);

    /**
     * 根据主键删除书籍
     *
     * @param id
     */
    @Delete("delete from book where id = #{id}")
    void deleteById(Long id);

    /**
     * 根据主键查询图书
     * @param bookId
     * @return
     */
    @Select("select * from book where id = #{bookId}")
    Book getBookByBookId(Long bookId);

    /**
     * 更新图书借阅数量
     * @param bookId
     */
    @Update("update book set borrowed = borrowed + 1 where id = #{bookId}")
    void updateBorrowedBook(Long bookId);

    /**
     * 更新图书归还数量
     * @param bookId
     */
    @Update("update book set borrowed = borrowed - 1 where id = #{bookId}")
    void updateReturnBook(Long bookId);

    /**
     * 根据主键查询借阅数量
     * @param id
     * @return
     */
    @Select("select borrowed from book where id = #{id}")
    int getBorrowedBybookid(Long id);

    /**
     * 搜索书籍
     * @param searchPageDTO
     * @return
     */
    Page<BookPageVO> searchPageQuery(SearchPageDTO searchPageDTO);

    /**
     * 根据分类获取图书统计
     * @return
     */
    List<BookCountByCategoryVO> getBookCountByCategory();

    /**
     * 根据年份获取图书统计
     * @return
     */
    List<BookCountByYearVO> getBookCountByPublishedYear();

    /**
     * 根据价格获取图书统计
     * @return
     */
    List<BookCountByPriceVO> getBookCountByPrice();

    /**
     * 获取借阅次数图书排名
     * @return
     */
    Page<BookCountByBorrowedVO> getBookCountByBorrowed(Integer limit);

    /**
     * 获取浏览次数图书排名
     * @return
     */
    Page<BookCountByBrowseVO> getBookCountByBrowse(Integer limit);

    /**
     * 根据推荐书籍id获取推荐书籍
     * @param recommendationsIds
     * @return
     */
    List<RecommendationsVO> getRecommendationsByBookIds(List<Long> recommendationsIds);

    /**
     * 根据标签id获取书籍
     * @param tagId
     * @return
     */
    Book getBookByTagId(Long tagId);
}
